﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Printing;
using System.Windows.Shapes;

namespace SL.WorkfowDesigner.Designer
{
    public class DesignerPrintManager
    {

        private PrintDocument document;
        private PrinterFallbackSettings settings;
        private double beforePrintRate;
        private UIElement printUIElement;
        private TransformGroup renderTransformer;
        private BitmapImage bi;
        private Slider zoomSlider;

        public DesignerPrintManager(UIElement _printUIElement,Slider _zoomSlider)
        {
            printUIElement = _printUIElement;
            zoomSlider = _zoomSlider;

            document = new PrintDocument();

            document.BeginPrint += documentImage_BeginPrint;
            document.PrintPage += documentImage_PrintPage;
            document.EndPrint += documentImage_EndPrint;

            settings = new PrinterFallbackSettings();

            settings.ForceVector = true;

            settings.OpacityThreshold = 0.5;

            beforePrintRate = zoomSlider.Value;
 
        }

        public void Print(string printFileName)
        {
            document.Print(printFileName, settings, false);
        }

        private void documentImage_BeginPrint(object sender, BeginPrintEventArgs e)
        {
            //bi = new WriteableBitmap(printUIElement, null);
            beforePrintRate = zoomSlider.Value;
        }

        private void documentImage_EndPrint(object sender, EndPrintEventArgs e)
        {
            TransformGroup tg = printUIElement.RenderTransform as TransformGroup;

            if (tg != null)
            {
                tg.Children.Clear();
            }
        }

        private void documentImage_PrintPage(object sender, PrintPageEventArgs e)
        {
            e.PageVisual = printUIElement;

            double rateh = e.PrintableArea.Height / bi.PixelHeight;

            double ratew = e.PrintableArea.Width / bi.PixelWidth;

            double rate = Math.Min(rateh, ratew);

            double frate = Math.Floor(rate / zoomSlider.SmallChange) * zoomSlider.SmallChange;
            //double frate = Math.Ceiling(rate / zoomSlider.SmallChange) * zoomSlider.SmallChange;

            renderTransformer = new TransformGroup();

            renderTransformer.Children.Add(new ScaleTransform() { CenterX = 0, CenterY = 0, ScaleX = frate, ScaleY = frate });
            //renderTransformer.Children.Add(new RotateTransform() { Angle = 90 });

            //如果长宽比不一致，
            if (!((bi.PixelWidth >= bi.PixelHeight) && (e.PrintableArea.Width >= e.PrintableArea.Height)))
            {
                renderTransformer.Children.Add(new RotateTransform() { Angle = 90 });

                renderTransformer.Children.Add(new TranslateTransform() { X = e.PrintableArea.Width });
            }

            printUIElement.RenderTransform = renderTransformer;

            e.HasMorePages = false;
        }
    }
}
